package com.xjt.javase.algorithm.bit_operation;

/**
 * 打印一个整型int 的4bytes -> 32bit
 */
public class PrintInt32 {
    public static void main(String[] args) {
        // 1 ---> 00000000 00000000 00000000 00000001
        // 2 ---> 00000000 00000000 00000000 00000010
        // 8562 --->00000000 00000000 00100001 01110010
        //          10000000 00000000 00000000 00000000       8562 & (1<<31) == 0
        //          01000000 00000000 00000000 00000000       8562 & (1<<30) == 0
        //          00100000 00000000 00000000 00000000       8562 & (1<<29) == 0
        //            ...
        //          00000000 00000000 00100000 00000000       8562 & (1<<14) == 1
        //            ...
        //          00000000 00000000 00000001 00000000       8562 & (1<<8) == 1


        //test01();

        print32(2);
        print32(4);
        print32(8562);
    }

    public static void print32(int num) {
        for (int i = 31; i >= 0; i--) {
            System.out.printf((num & (1 << i)) == 0 ? "0" : "1");
        }
        System.out.println();
    }

    public static void test01() {
        /*先来回顾一下位运算*/
        //00000010  4
        //00010000  4 << 2
        //00000000  0
        System.out.println(2 & (4 << 2));   //0
        //00000010
        //00000011
        System.out.println(2 & 3);      //2

        System.out.println((8562 & (1 << 8)) == 0 ? "0" : "1");     // 100000000
    }
}
